function asyncSquare(num) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      if (Math.random() > 0.5) {
        resolve(num * num);
      } else {
        reject(new Error(num));
      }
    }, 1000);
  });
}

// asyncSquare(2).then(res => {
//     console.log('res', res)
// }).catch(err => {
//     console.log('err', err)
// })

const arr = [1, 2, 3, 4, 5];

function loop(arr) {
  let current = arr.shift();
  if (current) {
    asyncSquare(current)
      .then(res => {
        console.log("res", res);
      })
      .catch(err => {
        console.log("err", err.message);
      })
      .finally(() => {
        loop(arr);
      });
  } else {
    console.log("complete!");
  }
}
loop(arr);
